home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 22 / PC Actual CD 22.iso / progs / DIRECTOR / data.z / Learning Director.dcr / UtilsCast_3_MS_ListIO.ls < prev    next >
Encoding:
Text File  |  1997-05-10  |  10.1 KB  |  324 lines

  1. global gListIOfileObjOrString
  2.  
  3. on getlist fileNameOrTextCast, cstlib
  4.   set propList to getaProp([:], 1)
  5.   set textCastNum to 0
  6.   set textCastLineNum to 1
  7.   set castLibNum to 1
  8.   set fileObj to 0
  9.   if integerp(fileNameOrTextCast) then
  10.     set textCastNum to fileNameOrTextCast
  11.   else
  12.     if char 1 of fileNameOrTextCast = "@" then
  13.       set textCastNum to the number of member chars(fileNameOrTextCast, 2, length(fileNameOrTextCast))
  14.     else
  15.       set fileObj to fileio(mnew, "read", fileNameOrTextCast)
  16.     end if
  17.   end if
  18.   if textCastNum > 0 then
  19.     if the paramCount = 2 then
  20.       set castLibNum to the number of castLib cstlib
  21.     else
  22.       set castLibNum to 1
  23.     end if
  24.   end if
  25.   if objectp(fileObj) or (textCastNum > 0) then
  26.     set MORE to 1
  27.     repeat while MORE
  28.       if objectp(fileObj) then
  29.         set lineString to fileObj(mReadLine)
  30.       else
  31.         set lineString to line textCastLineNum of the text of member textCastNum of castLib castLibNum
  32.         set textCastLineNum to textCastLineNum + 1
  33.       end if
  34.       if lineString = EMPTY then
  35.         set MORE to 0
  36.         next repeat
  37.       end if
  38.       set firstWord to word 1 of lineString
  39.       if firstWord <> EMPTY then
  40.         set lineString to chars(lineString, offset(firstWord, lineString), length(lineString))
  41.         set pos to length(lineString)
  42.         repeat while 1
  43.           set aChar to char pos of lineString
  44.           if (aChar = " ") or (aChar = TAB) or (aChar = RETURN) then
  45.             set pos to pos - 1
  46.             next repeat
  47.           end if
  48.           exit repeat
  49.         end repeat
  50.         set lineString to chars(lineString, 1, pos)
  51.         set fileContents to fileContents & lineString
  52.       end if
  53.     end repeat
  54.     if objectp(fileObj) then
  55.       fileObj(mdispose)
  56.     end if
  57.     set propList to value(fileContents)
  58.   end if
  59.   return propList
  60. end
  61.  
  62. on ImportTextInto fileName, memNum, castLibNum
  63.   set fileObj to fileio(mnew, "read", fileName)
  64.   if objectp(fileObj) then
  65.     set s to fileObj(mReadFile)
  66.     fileObj(mdispose)
  67.   else
  68.     set s to EMPTY
  69.   end if
  70.   if the paramCount = 3 then
  71.     if stringp(castLibNum) then
  72.       set castLibNum to the number of castLib castLibNum
  73.     end if
  74.   else
  75.     set castLibNum to 1
  76.   end if
  77.   duplicate(member "empty#field", member memNum of castLib castLibNum)
  78.   set the text of member memNum of castLib castLibNum to s
  79. end
  80.  
  81. on outputLine lineString, fileObjOrString, formatted
  82.   if objectp(fileObjOrString) then
  83.     if formatted then
  84.       set lineString to lineString & RETURN
  85.     end if
  86.     fileObjOrString(mWriteString, lineString)
  87.     if fileObjOrString(mStatus) <> 0 then
  88.       return 0
  89.     end if
  90.   else
  91.     if stringp(fileObjOrString) then
  92.       set gListIOfileObjOrString to gListIOfileObjOrString & lineString
  93.       if formatted then
  94.         set gListIOfileObjOrString to gListIOfileObjOrString & RETURN
  95.       end if
  96.     else
  97.       put lineString
  98.     end if
  99.   end if
  100.   return 1
  101. end
  102.  
  103. on putlist propList, fileNameOrTextCast, cstlib
  104.   set castLibNum to 1
  105.   set textCastNum to EMPTY
  106.   if (paramCount() > 1) and not symbolp(fileNameOrTextCast) then
  107.     if integerp(fileNameOrTextCast) then
  108.       set textCastNum to fileNameOrTextCast
  109.       set gListIOfileObjOrString to EMPTY
  110.     else
  111.       if char 1 of fileNameOrTextCast = "@" then
  112.         set textCastNum to the number of member chars(fileNameOrTextCast, 2, length(fileNameOrTextCast))
  113.         set gListIOfileObjOrString to EMPTY
  114.       else
  115.         set gListIOfileObjOrString to fileio(mnew, "write", fileNameOrTextCast)
  116.         if objectp(gListIOfileObjOrString) = 0 then
  117.           return 0
  118.         end if
  119.       end if
  120.     end if
  121.   else
  122.     set gListIOfileObjOrString to 0
  123.   end if
  124.   if integerp(textCastNum) then
  125.     if the paramCount >= 2 then
  126.       set castLibNum to the number of castLib cstlib
  127.     else
  128.       set castLibNum to 1
  129.     end if
  130.     if textCastNum < 0 then
  131.       set createTextCast to 1
  132.     else
  133.       if the type of member textCastNum of castLib cstlib = #empty then
  134.         set createTextCast to 1
  135.       else
  136.         set createTextCast to 0
  137.       end if
  138.     end if
  139.   end if
  140.   set formatted to not (param(paramCount()) = #unformatted)
  141.   set listWalker to [0: 0]
  142.   set currList to propList
  143.   set currListPos to 1
  144.   set thisDepth to 0
  145.   set tabString to "    "
  146.   if not outputLine("[", gListIOfileObjOrString, formatted) then
  147.     return 0
  148.   end if
  149.   repeat while listp(currList)
  150.     set lineString to EMPTY
  151.     if formatted then
  152.       repeat with depthCount = 0 to thisDepth
  153.         set lineString to lineString & tabString
  154.       end repeat
  155.     end if
  156.     if currListPos <= count(currList) then
  157.       if ilk(currList, #propList) then
  158.         set prop to getPropAt(currList, currListPos)
  159.         if ilk(prop, #symbol) then
  160.           set lineString to lineString & "#" & string(prop) & " : "
  161.         else
  162.           if ilk(prop, #string) then
  163.             set lineString to lineString & QUOTE & string(prop) & QUOTE & " : "
  164.           else
  165.             set lineString to lineString & string(prop) & " : "
  166.           end if
  167.         end if
  168.       end if
  169.       set isListValue to 0
  170.       set value to getAt(currList, currListPos)
  171.       if ilk(value, #propList) or ilk(value, #linearList) then
  172.         set isListValue to 1
  173.         addProp(listWalker, currList, currListPos + 1)
  174.         set currList to value
  175.         set currListPos to 1
  176.         set thisDepth to thisDepth + 1
  177.         set lineString to lineString & "["
  178.       else
  179.         if ilk(value, #string) then
  180.           if paramCount() > 1 then
  181.             set strLen to length(value)
  182.             set startPos to 1
  183.             repeat with endPos = 1 to strLen
  184.               set c to char endPos of value
  185.               if c = QUOTE then
  186.                 set lineString to lineString & QUOTE & chars(value, startPos, endPos - 1) & QUOTE & ""E"
  187.                 set startPos to endPos + 1
  188.                 if endPos < strLen then
  189.                   set lineString to lineString & "&"
  190.                 end if
  191.                 next repeat
  192.               end if
  193.               if c = RETURN then
  194.                 set lineString to lineString & QUOTE & chars(value, startPos, endPos - 1) & QUOTE & "&RETURN"
  195.                 set startPos to endPos + 1
  196.                 if endPos < strLen then
  197.                   set lineString to lineString & "&"
  198.                 end if
  199.                 next repeat
  200.               end if
  201.             end repeat
  202.             if (strLen = 0) or (startPos < endPos) then
  203.               set lineString to lineString & QUOTE & chars(value, startPos, endPos - 1) & QUOTE
  204.             end if
  205.           else
  206.             set lineString to lineString & QUOTE & string(value) & QUOTE
  207.           end if
  208.           set currListPos to currListPos + 1
  209.         else
  210.           if ilk(value, #symbol) then
  211.             set lineString to lineString & "#" & string(value)
  212.             set currListPos to currListPos + 1
  213.           else
  214.             set lineString to lineString & string(value)
  215.             set currListPos to currListPos + 1
  216.           end if
  217.         end if
  218.       end if
  219.       if (currListPos <= count(currList)) and not isListValue then
  220.         set lineString to lineString & ","
  221.       end if
  222.       if not outputLine(lineString, gListIOfileObjOrString, formatted) then
  223.         return 0
  224.       end if
  225.       next repeat
  226.     end if
  227.     if ilk(currList, #propList) then
  228.       if count(currList) = 0 then
  229.         set lineString to lineString & " : "
  230.         if not outputLine(lineString, gListIOfileObjOrString, formatted) then
  231.           return 0
  232.         end if
  233.       end if
  234.     end if
  235.     set currList to getPropAt(listWalker, count(listWalker))
  236.     set currListPos to getAt(listWalker, count(listWalker))
  237.     set thisDepth to thisDepth - 1
  238.     set lineString to EMPTY
  239.     if thisDepth >= 0 then
  240.       deleteAt(listWalker, count(listWalker))
  241.       if formatted then
  242.         repeat with depthCount = 0 to thisDepth
  243.           set lineString to lineString & tabString
  244.         end repeat
  245.       end if
  246.       set lineString to lineString & "]"
  247.       if currListPos <= count(currList) then
  248.         set lineString to lineString & ","
  249.       end if
  250.       if not outputLine(lineString, gListIOfileObjOrString, formatted) then
  251.         return 0
  252.       end if
  253.     end if
  254.   end repeat
  255.   if not outputLine("]", gListIOfileObjOrString, formatted) then
  256.     return 0
  257.   end if
  258.   if objectp(gListIOfileObjOrString) then
  259.     gListIOfileObjOrString(mdispose)
  260.   else
  261.     if stringp(gListIOfileObjOrString) then
  262.       if createTextCast then
  263.         set textCastNum to findEmpty(member 1 of castLib castLibNum)
  264.         duplicate(member "empty#field", member textCastNum of castLib castLibNum)
  265.         set the name of member textCastNum of castLib castLibNum to chars(fileNameOrTextCast, 2, length(fileNameOrTextCast))
  266.       end if
  267.       set the text of member textCastNum of castLib castLibNum to gListIOfileObjOrString
  268.       set gListIOfileObjOrString to 0
  269.     end if
  270.   end if
  271.   return 1
  272. end
  273.  
  274. on FindPropList propList, listName
  275.   set listWalker to [0: 0]
  276.   set currList to propList
  277.   set currListPos to 1
  278.   set linearSearch to 0
  279.   repeat while currList <> 0
  280.     if currListPos <= count(currList) then
  281.       set match to 0
  282.       if linearSearch = 0 then
  283.         set currListPos to findPos(currList, listName)
  284.         if currListPos >= 1 then
  285.           set match to 1
  286.         else
  287.           set currListPos to 1
  288.         end if
  289.       else
  290.         set keyString to getPropAt(currList, currListPos)
  291.         if keyString = listName then
  292.           set match to 1
  293.         end if
  294.       end if
  295.       set value to getAt(currList, currListPos)
  296.       if match = 1 then
  297.         if ilk(value, #propList) then
  298.           return value
  299.         else
  300.           set currListPos to currListPos + 1
  301.           set linearSearch to 1
  302.         end if
  303.       else
  304.         if ilk(value, #propList) then
  305.           addProp(listWalker, currList, currListPos + 1)
  306.           set currList to getAt(currList, currListPos)
  307.           set currListPos to 1
  308.           set linearSearch to 0
  309.         else
  310.           set currListPos to currListPos + 1
  311.           set linearSearch to 1
  312.         end if
  313.       end if
  314.       next repeat
  315.     end if
  316.     set currList to getPropAt(listWalker, count(listWalker))
  317.     set currListPos to getAt(listWalker, count(listWalker))
  318.     if currList <> 0 then
  319.       deleteAt(listWalker, count(listWalker))
  320.     end if
  321.   end repeat
  322.   return 0
  323. end
  324.